package com.syg.FileUpload;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 使用commons-fileupload组件上传文件,该组件是Apache的一个开源项目之一
 * 根据传智视频改编而来
 */
@WebServlet("/commonsfileupload2")
public class _02_Commons_fileupload extends HttpServlet{

    private final String uploadDir = "D:/Cache/DelTrash/images";//指定上传文件路径

    /*********文件上传组件： 处理文件上传************/
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1,首先判断是否为文件上传表单
        boolean multipartFlag = ServletFileUpload.isMultipartContent(req);
        if (!multipartFlag) {
            throw new IllegalArgumentException("上传内容不是multipart/form-data类型.");//这里就不能写return了
        }
        //2,文件上传工厂
        FileItemFactory factory = new DiskFileItemFactory();
        //3,创建文件上传核心工具类
        ServletFileUpload upload = new ServletFileUpload(factory);
        //一,设置单个文件允许的最大大小:30M
        upload.setFileSizeMax(30*1024*1024);
        //二,设置文件上传表单允许的总大小:80M
        upload.setSizeMax(80*1024*1024);
        //三,设置上传表单文件名的编码
        upload.setHeaderEncoding("UTF-8");
        try {
            //4,把请求数据转换成一个FileItem集合对象(在请求当中获取所有表单项)
            List<FileItem> list = upload.parseRequest(req);

            //5,遍历得到每一个表单项
            for (FileItem fileItem : list) {
                //a,判断是否是普通表单项
                if (fileItem.isFormField()) {
                    long size = fileItem.getSize();             //获取普通表单项的大小
                    String string = fileItem.getString();       //获取表单value值
                    String fieldName = fileItem.getFieldName(); //表单项名称(表单项name属性值)
                    System.out.println("表单name是>>" + fieldName + ",表单value是>>" + string);
                }
                //b,上传表单项
                else{
                    String fieldName = fileItem.getFieldName(); //表单项name属性值(不是上传的文件名)
                    String string = fileItem.getString();       //获取表单value值
                    String name = fileItem.getName();           //获取上传的文件名(包含扩展名,不是表单项的name属性值)
                    long size = fileItem.getSize();             //获取上传文件的大小
                    String contentType = fileItem.getContentType();

                    /**
                     * 6,处理文件重名
                     * 后台处理:给用户添加一个唯一标记!
                     */
                    String uuid = UUID.randomUUID().toString();//c,随机生成一个唯一标识
                    File file = new File(uploadDir,uuid+"#"+name);//创建目标文件
                    fileItem.write(file);//文件上传<==>fileItem.getInputStream(),然后在手动处理
                    fileItem.delete();//删除系统产生的临时文件
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
